<template>
  <div
    :class="prefixCls"
    class="h-[100%] relative <xl:bg-v-dark <sm:px-10px <xl:px-10px <md:px-10px"
  >
    <div class="relative h-full flex mx-auto"> </div>
  </div>
</template>
<script lang="ts" setup>
import { useDesign } from '@/hooks/web/useDesign'

defineOptions({ name: 'Login' })
import { ElLoading } from 'element-plus'

import * as authUtil from '@/utils/auth'
import * as LoginApi from '@/api/login'
import { Base64 } from 'js-base64'
const { getPrefixCls } = useDesign()
const prefixCls = getPrefixCls('login')

// 检查跳转url是否要自动登录
const checkAutoLogin = async () => {
  try {
    console.log('自动登录操作，清除所有缓存')
    authUtil.clear()
    let url = window.location.href
    console.log('自动登录请求 URL=' + url)
    const auto = window.location.search.replace('?auto=', '')
    // url中auto请求参数base64解码
    const decode = Base64.decode(auto)
    console.log('自动登录请求 decode=' + decode)
    const dataParam = JSON.parse(decode)
    // const tenantId = await LoginApi.getTenantIdByName(dataParam.tenantName)
    // authUtil.setTenantId(tenantId)
    const res = await LoginApi.loginV2(dataParam)
    authUtil.setToken(res)
    const href = url.replace('/autoLogin', '/login')
    console.log(href)
    // 缓存退出登录时，回调url
    authUtil.setTenantName(dataParam.tenantName)
    // 明文密码清空
    dataParam.password = ''
    authUtil.setAutoLoginParam(dataParam)
    window.location.href = href
  } catch {
    // 自动登陆失败回到来源页
    const autoLoginParam = authUtil.getAutoLoginParam()
    window.location.href = autoLoginParam ? autoLoginParam.path : document.referrer
  } finally {
    setTimeout(() => {
      const loadingInstance = ElLoading.service()
      loadingInstance.close()
    }, 400)
  }
}
onMounted(() => {
  checkAutoLogin()
})
</script>

<style lang="scss" scoped>
$prefix-cls: #{$namespace}-login;

.#{$prefix-cls} {
  &__left {
    &::before {
      position: absolute;
      top: 0;
      left: 0;
      z-index: -1;
      width: 100%;
      height: 100%;
      background-image: url('@/assets/svgs/login-bg.svg');
      background-position: center;
      background-repeat: no-repeat;
      content: '';
    }
  }
}
</style>
